package com.fowo.api.service.handleWork;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.StockOutProduct;
import com.fowo.api.model.stock.in.StockInVo;
import com.fowo.api.model.stock.in.product.StockInProductItemVo;
import com.fowo.api.model.stock.out.StockOutPushStockInAndStockOutAction;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.service.impl.StockInProductServiceImpl;
import com.fowo.api.service.impl.StockInServiceImpl;
import com.fowo.api.service.impl.StockOutProductServiceImpl;
import com.fowo.api.service.impl.StockOutServiceImpl;
import com.fowo.api.sys.entity.SysDepartment;
import com.fowo.api.sys.service.impl.SysDepartmentServiceImpl;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class stockOutPushStockInAndStockOut implements StockOutPushStockInAndStockOutAction {

    @Resource
    private StockOutServiceImpl stockOutServiceImpl;
    @Resource
    private StockOutProductServiceImpl stockOutProductService;
    @Resource
    private StockInServiceImpl stockInService;
    @Resource
    protected StockInProductServiceImpl stockInProductService;
    @Resource
    private SysDepartmentServiceImpl sysDepartmentService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void run(Long id) throws Exception {
        StockOutVo voById = stockOutServiceImpl.getVoById(id);
        Assert.notNull(voById,"未查到!");
        List<StockOutProduct> list = stockOutProductService.list(Wrappers.<StockOutProduct>query().lambda().eq(StockOutProduct::getStockOutId, id));
        SysDepartment department = getDepartment();
        addStockIn(voById,list,department);
        addStockOut(voById,department);
    }

    private void addStockIn(StockOutVo voById, List<StockOutProduct>list,SysDepartment department){
        StockInVo model =new StockInVo();
        model.setStatus("0");
        String Remark=",销售出库单:"+voById.getOrderSn();
        model.setRemarks(voById.getRemark()!=null?voById.getRemark()+Remark:Remark);
//        model.setRemarks(voById.getRemark());
        model.setApplicationDep(department.getId());
        model.setApplicationDepName1(department.getName());
        model.setWarehouse(voById.getWarehouse());
        model.setPurchasingOrg(voById.getNeedOrg());
        model.setNeedOrg(voById.getNeedOrg());
        model.setWarehousingDate(new Date());
        model.setReceiptType("RK001");
        model.setAddType(voById.getAddType());
        List<StockInProductItemVo> productList=new ArrayList<>();
        for(int i=0;i<list.size();i++){
            StockInProductItemVo stockInProductItemVo=new StockInProductItemVo();
            StockOutProduct stockOutProduct = list.get(i);
            stockInProductItemVo.setProductId(stockOutProduct.getProductId());
            stockInProductItemVo.setSku(stockOutProduct.getSku());
            stockInProductItemVo.setProductPrice(stockOutProduct.getPrice());
            stockInProductItemVo.setProductName(stockOutProduct.getProductName());
            stockInProductItemVo.setQuantity(stockOutProduct.getQuantity());
            stockInProductItemVo.setWarehouseId(model.getWarehouse());
            stockInProductItemVo.setUnitPrice(stockOutProduct.getPrice());
            stockInProductItemVo.setSubtotal(stockOutProduct.getSubtotal());
            productList.add(stockInProductItemVo);
        }
        model.setProductList(productList);
        try {
            stockInService.create(model);
        } catch (Exception e) {
            throw new RuntimeException("");
        }
    }

    private void addStockOut(StockOutVo voById,SysDepartment department){
        try {
            String Remark=",销售出库单:"+voById.getOrderSn();
            voById.setRemark(voById.getRemark()!=null?voById.getRemark()+Remark:Remark);
            voById.setId(null);
            voById.setStatus("CS01");
            voById.setOrderSn(null);
            voById.setType("CK002");
            voById.setDept(department.getId());
            voById.getStockOutProducts().forEach(x->{
                x.setStockOutId(null);
            });
            stockOutServiceImpl.create(voById);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //获取部门信息
    private SysDepartment  getDepartment(){
        SysDepartment dep = sysDepartmentService.getOne(Wrappers.<SysDepartment>query()
                .lambda().eq(SysDepartment::getName, "泽壹"));
        return dep;
    }



}
